package com.longrise.android.byjk.plugins.tabthird;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.MineMultipleitem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.datasource.local.sp.CacheUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/3/9.
 * 我的页面presenter
 */

public class MinePresenter extends MineContract.Presenter {

    private static final String TAG = "MinePresenter";

    @Override
    protected void init() {
        //mView.setRefreshing(true);
        //  requestMineCustomerModule(true);
        requestHeadIcon();
//        requestVip();
        requestName();
//        requestBbye();
       // requestMyCourse();
    }

    @Override
    void refreshAll() {
    //    mView.setRefreshing(true);
        //    requestMineCustomerModule(false);
        requestHeadIcon();
//        requestVip();
        requestName();
//        requestBbye();
       // requestMyCourse();
    }

    @Override
    void refreshCustomerModule() {
        //     requestMineCustomerModule(false);
    }

    @Override
    void refreshHeadIcon() {
        requestHeadIcon();
    }

//    @Override
//    void refreshVipIcon() {
//        requestVip();
//    }

    @Override
    void refreshName() {
        requestName();
    }

//    @Override
//    void refreshBbye() {
//        requestBbye();
//    }

    /**
     * 我的课程的数据 3条
     */
    private void requestMyCourse() {
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_course_sMyCourseBeans", bean1, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            EntityBean[]  coursebeans = (EntityBean[]) bean.get("result");
                            mView.refreshMycourse(coursebeans);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mView.refreshComplete();
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }
                });
    }


    /**
     * 我的页面底部 常用工具或者我的问答数据
     *
     * @param isFirst 是否是第一次请求
     */
    private void requestMineCustomerModule(boolean isFirst) {
        if (isFirst) {
            //先读缓存 预加载
            EntityBean[] bbt_sMyCustomerModule = CacheUtils.getCacheFirstData("bbt_sMyCustomerModule");
            if (bbt_sMyCustomerModule != null) {
                parseMineCustomerModule(bbt_sMyCustomerModule);
            }
        }
        EntityBean bean = new EntityBean();
        bean.set("apptype", "android");
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_sMyCustomerModule", fbean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        EntityBean[] resultBeans = bean.getBeans("result");
                        parseMineCustomerModule(resultBeans);
                        //做缓存
                        CacheUtils.cacheFirstData("bbt_sMyCustomerModule", resultBeans);
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                mView.refreshComplete();
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                mView.showToast(AppUtil.getString(R.string.networkerror));
            }
        });
    }

    private void parseMineCustomerModule(EntityBean[] beans) {
        List<MineMultipleitem> list = new ArrayList<>();
        if (beans != null) {
            for (EntityBean bean : beans) {
                String style = bean.getString("style");
                switch (style) {
                    case "1007": //类似我的问答 横排是三个item
                        list.add(new MineMultipleitem(MineMultipleitem.TYPE1));
                        break;
                    case "1008":  //类似常用工具
                        list.add(new MineMultipleitem(MineMultipleitem.TYPE2));
                        break;
                }
            }
            mView.refreshCustomerModule(beans, list);
        }
    }

    /**
     * 获取头像地址
     */
    private void requestHeadIcon() {
        String userHeadIcon = UserInfor.getInstance().getUserHeadIcon();
        mView.refreshHeadIcon(userHeadIcon);
    }

//    /**
//     * 判断当前vip等级
//     */
//    private void requestVip() {
//        int viptype = UserInfor.getInstance().getViptype();
//        switch (viptype) {
//            case VipType.VIP_1: //普通会员
//                mView.refreshVip(R.drawable.ic_my_member_normal);
//                break;
//            case VipType.VIP_3: //至尊会员
//                mView.refreshVip(R.drawable.ic_my_member_pro);
//                break;
//        }
//    }

    /**
     * 获取姓名
     */
    private void requestName() {
        String userNickName = UserInfor.getInstance().getUserNickName();
        mView.refreshName(userNickName);
    }

//    /**
//     * 获取保宝余额 我的积分
//     */
//    private void requestBbye() {
//        EntityBean bean = new EntityBean();
//        bean.set("userid", UserInfor.getInstance().getUserid());
//        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
//        EntityBean fbean = new EntityBean();
//        fbean.set("bean", bean);
//        LoadDataManager.getInstance(mContext).callService(null, UrlConstants.BaseUrl, "bbt_fee_sMyAccounts", fbean, new LoadDataManager.OnRequestCallListener() {
//            @Override
//            public void onSuccess(String key, String service, Object result) {
//                try {
//                    parseAccountResult(result);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onFinished(String key, String service) {
//
//            }
//        });
//    }

//    private void parseAccountResult(Object result) {
//        EntityBean bean = (EntityBean) result;
//        EntityBean resultbean = bean.getBean("result");
//        int resultstate = bean.getInt("resultstate");
//        String resultdesc = bean.getString("resultdesc");
//        if (resultstate == 1) {
//            String bbacount = resultbean.getString("bbacount");
//            String vipscores = resultbean.getString("vipscores");
//            mView.refreshBBye(bbacount, vipscores);
//            PrintLog.e(TAG, "bbacount: " + bbacount + "  vipscores: " + vipscores);
//        } else {
//            mView.showToast(resultdesc);
//        }
//    }
}
